package com.wtc.smallchangesys;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

/**
 * 该类是完成零钱通的各个功能的类
 * 使用OOP(面向对象编程)
 * 将各个功能对应一个方法。
 */
public class SmallChangeSysOOP {
    boolean loop = true;
    double balance = 0;
    Date date = null;
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd  HH:mm");
    String details = "";
    Scanner scanner = new Scanner(System.in);
    public void mainMenu() {
        do {
            System.out.println("--------------零钱通(OOP)-------------");
            System.out.println("\t\t\t\t\t\t1  零钱通明细");
            System.out.println("\t\t\t\t\t\t2  收 益 入 账");
            System.out.println("\t\t\t\t\t\t3  消            费");
            System.out.println("\t\t\t\t\t\t4  退            出");
            System.out.println("---------------------------------------");
            System.out.print("请输入相应选项的序号(1-4)：");
            int key = scanner.nextInt();
            switch (key) {
                case 1:
                    details();
                    break;
                case 2:
                    income();
                    break;
                case 3:
                    pay();
                    break;
                case 4:
                    exit();
                    break;
                default:
                    System.out.println("你输入的不是序号或者序号有误！请重新输入！");
            }
        } while (loop);
    }
    public void details() {
        System.out.println("---------------零钱通明细--------------");
        System.out.println(details);
    }
    public void income() {
        System.out.print(" 请输入入账金额：");
        double money = scanner.nextDouble();
        if (money <= 0) {
            System.out.println("收益金额需大于零！");
            return;
        }
        balance += money;
        date = new Date();
        details += "收益入账:\t+" + money + "\t\t" + simpleDateFormat.format(date) + "\t\t余额:" + balance + "\n";
    }
    public void pay() {
        System.out.print(" 请输入消费金额：");
        double money = scanner.nextInt();
        System.out.print(" 请输入消费信息：");
        String note = scanner.next();
        if (money <= 0 || money > balance) {
            System.out.println("消费金额需大于零且不能大于余额！");
            return;
        }
        balance -= money;
        date = new Date();
        details += note + "\t-" + money + "\t\t" + simpleDateFormat.format(date) + "\t\t余额:" + balance + "\n";
    }
    public void exit() {
        String choice = "";
        while (true) {
            System.out.print("你确定要退出系统吗？y/n");
            choice = scanner.next();
            if ("y".equals(choice) || "n".equals(choice)) {
                break;
            }
        }
        if (choice.equals("y")) {
            loop = false;
        }
        System.out.println("---------------系统退出----------------");
    }
}
